//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "FdmHullWhiteMesher.h"
using namespace Cephei::QL::Experimental::Finitedifferences;
#include <gen/QL/Processes/HullWhiteProcess.h>
#include <gen/QL/Experimental/Finitedifferences/Fdm1dMesher.h>
using namespace Cephei::QL::Processes;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Finitedifferences::CFdmHullWhiteMesher::CFdmHullWhiteMesher (UInt64 size, Cephei::QL::Processes::IHullWhiteProcess^ process, Double maturity, Microsoft::FSharp::Core::FSharpOption<UInt64>^ tAvgSteps, Microsoft::FSharp::Core::FSharpOption<Double>^ epsilon) : CFdm1dMesher(CFdmHullWhiteMesher::typeid)
{
    CHullWhiteProcess^ _Cprocess;
    try
    {
#ifdef HANDLE
        _phFdmHullWhiteMesher = NULL;
#endif
        QuantLib::Size _size = (QuantLib::Size)ValueHelper::Convert (size);
        _Cprocess = safe_cast<CHullWhiteProcess^> (process);
        _Cprocess->Lock();
        boost::shared_ptr<QuantLib::HullWhiteProcess>& _process = static_cast<boost::shared_ptr<QuantLib::HullWhiteProcess>&> (_Cprocess->GetShared ()); 
        QuantLib::Time _maturity = (QuantLib::Time)ValueHelper::Convert (maturity);
        QuantLib::Size _tAvgSteps = 
            (Microsoft::FSharp::Core::FSharpOption<UInt64>::IsSome::get (tAvgSteps) ? (QuantLib::Size)ValueHelper::Convert (tAvgSteps->Value) : 10); //4
        QuantLib::Real _epsilon = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (epsilon) ? (QuantLib::Real)ValueHelper::Convert (epsilon->Value) : 0.0001); //4
        _ppFdmHullWhiteMesher = new boost::shared_ptr<QuantLib::FdmHullWhiteMesher> (new QuantLib::FdmHullWhiteMesher ( _size,  _process,  _maturity,  _tAvgSteps,  _epsilon ));
        SetFdm1dMesher (boost::dynamic_pointer_cast<QuantLib::Fdm1dMesher> (*_ppFdmHullWhiteMesher));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cprocess != nullptr) _Cprocess->Unlock();
    }
}
Cephei::QL::Experimental::Finitedifferences::CFdmHullWhiteMesher::CFdmHullWhiteMesher (boost::shared_ptr<QuantLib::FdmHullWhiteMesher>& childNative, Object^ owner) : CFdm1dMesher(CFdmHullWhiteMesher::typeid)
{
#ifdef HANDLE
	_phFdmHullWhiteMesher = NULL;
#endif
	_ppFdmHullWhiteMesher = &childNative;
    _ppFdm1dMesher = new boost::shared_ptr<QuantLib::Fdm1dMesher> (boost::dynamic_pointer_cast<QuantLib::Fdm1dMesher> (*_ppFdmHullWhiteMesher));
}
Cephei::QL::Experimental::Finitedifferences::CFdmHullWhiteMesher::CFdmHullWhiteMesher (QuantLib::FdmHullWhiteMesher& childNative, Object^ owner) : CFdm1dMesher(CFdmHullWhiteMesher::typeid)
{
#ifdef HANDLE
	_phFdmHullWhiteMesher = NULL;
#endif
	_ppFdmHullWhiteMesher = new boost::shared_ptr<QuantLib::FdmHullWhiteMesher> (&childNative);
    _ppFdm1dMesher = new boost::shared_ptr<QuantLib::Fdm1dMesher> (boost::dynamic_pointer_cast<QuantLib::Fdm1dMesher> (*_ppFdmHullWhiteMesher));
    _FdmHullWhiteMesherOwner = owner;
    _Fdm1dMesherOwner = owner;
}

Cephei::QL::Experimental::Finitedifferences::CFdmHullWhiteMesher::CFdmHullWhiteMesher (CFdmHullWhiteMesher^ copy) : CFdm1dMesher(CFdmHullWhiteMesher::typeid)
{
#ifdef HANDLE
	_phFdmHullWhiteMesher = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppFdmHullWhiteMesher = new boost::shared_ptr<QuantLib::FdmHullWhiteMesher> (copy->GetShared());
        _ppFdm1dMesher = new boost::shared_ptr<QuantLib::Fdm1dMesher> (boost::dynamic_pointer_cast<QuantLib::Fdm1dMesher> (*_ppFdmHullWhiteMesher));
    }
}
Cephei::QL::Experimental::Finitedifferences::CFdmHullWhiteMesher::CFdmHullWhiteMesher (System::Type^ t) : CFdm1dMesher(CFdmHullWhiteMesher::typeid)
{
#ifdef HANDLE
	_phFdmHullWhiteMesher = NULL;
#endif
	if (!t->IsSubclassOf(CFdmHullWhiteMesher::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Finitedifferences::CFdmHullWhiteMesher::CFdmHullWhiteMesher (QuantLib::Handle<QuantLib::FdmHullWhiteMesher>& childNative, Object^ owner)  : CFdm1dMesher(CFdmHullWhiteMesher::typeid)
{
	_phFdmHullWhiteMesher = &childNative;
	_ppFdmHullWhiteMesher = &static_cast<boost::shared_ptr<QuantLib::FdmHullWhiteMesher>>(childNative.currentLink());
    _ppFdm1dMesher = new boost::shared_ptr<QuantLib::Fdm1dMesher> (boost::dynamic_pointer_cast<QuantLib::Fdm1dMesher> (*_ppFdmHullWhiteMesher));
    _FdmHullWhiteMesherOwner = owner;
}
Cephei::QL::Experimental::Finitedifferences::CFdmHullWhiteMesher::CFdmHullWhiteMesher (QuantLib::Handle<QuantLib::FdmHullWhiteMesher> childNative)  : CFdm1dMesher(CFdmHullWhiteMesher::typeid)
{
	_phFdmHullWhiteMesher = &childNative;
	_ppFdmHullWhiteMesher = &static_cast<boost::shared_ptr<QuantLib::FdmHullWhiteMesher>>(childNative.currentLink());
    _ppFdm1dMesher = new boost::shared_ptr<QuantLib::Fdm1dMesher> (boost::dynamic_pointer_cast<QuantLib::Fdm1dMesher> (*_ppFdmHullWhiteMesher));
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Finitedifferences::CFdmHullWhiteMesher::CFdmHullWhiteMesher (QuantLib::FdmHullWhiteMesher childNative)  : CFdm1dMesher(CFdmHullWhiteMesher::typeid)
{
#ifdef HANDLE
	_phFdmHullWhiteMesher = NULL;
#endif
	_ppFdmHullWhiteMesher = new boost::shared_ptr<QuantLib::FdmHullWhiteMesher> (new QuantLib::FdmHullWhiteMesher (childNative));
    _ppFdm1dMesher = new boost::shared_ptr<QuantLib::Fdm1dMesher> (boost::dynamic_pointer_cast<QuantLib::Fdm1dMesher> (*_ppFdmHullWhiteMesher));
}
#endif

Cephei::QL::Experimental::Finitedifferences::CFdmHullWhiteMesher::~CFdmHullWhiteMesher ()
{
    if (_ppFdmHullWhiteMesher != NULL)
    {
	    delete _ppFdmHullWhiteMesher;
        _ppFdmHullWhiteMesher = NULL;
    }
}
Cephei::QL::Experimental::Finitedifferences::CFdmHullWhiteMesher::!CFdmHullWhiteMesher ()
{
    if (_ppFdmHullWhiteMesher != NULL)
    {
	    delete _ppFdmHullWhiteMesher;
    }
}
QuantLib::FdmHullWhiteMesher& Cephei::QL::Experimental::Finitedifferences::CFdmHullWhiteMesher::GetReference ()
{
    if (_ppFdmHullWhiteMesher == NULL) throw gcnew NativeNullException ();
	return **_ppFdmHullWhiteMesher;
}
boost::shared_ptr<QuantLib::FdmHullWhiteMesher>& Cephei::QL::Experimental::Finitedifferences::CFdmHullWhiteMesher::GetShared ()
{
    if (_ppFdmHullWhiteMesher == NULL) throw gcnew NativeNullException ();
	return *_ppFdmHullWhiteMesher;
}
QuantLib::FdmHullWhiteMesher* Cephei::QL::Experimental::Finitedifferences::CFdmHullWhiteMesher::GetPointer ()
{
    if (_ppFdmHullWhiteMesher == NULL) throw gcnew NativeNullException ();
	return &**_ppFdmHullWhiteMesher;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::FdmHullWhiteMesher>& Cephei::QL::Experimental::Finitedifferences::CFdmHullWhiteMesher::GetHandle ()
{
	if (_phFdmHullWhiteMesher == NULL)
	{
		_phFdmHullWhiteMesher = new Handle<QuantLib::FdmHullWhiteMesher> (*_ppFdmHullWhiteMesher);
	}
	return *_phFdmHullWhiteMesher;
}
#endif
bool Cephei::QL::Experimental::Finitedifferences::CFdmHullWhiteMesher::HasNative () 
{
	return (_ppFdmHullWhiteMesher != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Experimental::Finitedifferences::IFdmHullWhiteMesher^ Cephei::QL::Experimental::Finitedifferences::CFdmHullWhiteMesher_Factory::Create (UInt64 size, Cephei::QL::Processes::IHullWhiteProcess^ process, Double maturity, Microsoft::FSharp::Core::FSharpOption<UInt64>^ tAvgSteps, Microsoft::FSharp::Core::FSharpOption<Double>^ epsilon)
{
    return gcnew CFdmHullWhiteMesher ( size,  process,  maturity,  tAvgSteps,  epsilon);
}
